/*
 * author zby
 * 首页初始化数据
 */

(function (document, window, $, Utils) {
    'use strict';
    var vm, token;
    var areaId;
    $(function () {
        var fns = {
            //选择企业类型
            chooseType: function (me) {
                const val = $(me).val();
                const companyName = $(me).parent().parent().next();
                $(companyName).css("display", val == 'SIGN_IN_TYPE_ENTERPRISE' ? "block" : "none")
            },
            sendContact: function (me) {
                const form = $(me).parents("form");
                const elements = $(form).find(".form-control");
                const flag = Utils.validForm(elements);
                if (flag) {
                    $.ajax({
                        url: constant.server_path + "/front/contact",
                        async: true,
                        timeout: 3000,
                        type: "POST",
                        crossDomain: true,
                        cache: false,
                        dataType: "json",//预期服务器返回的数据类型
                        data: $("#contactForm").serialize(),
                        success: function (data) {
                            if (data.success) {
                                layer.msg("提交成功，手机请保持畅通，如果信息正确，会在两天内给你信息", {time: 1000, icon: 1}, function () {
                                  window.location.href="index.html"
                                });
                            } else {
                                layer.msg(data.errorCtx.errorMsg, {time: 2000, icon: 2}, function () {
                                    window.location.href="index.html"
                                });
                            }
                        },
                        fail: function () {
                            layer.msg("网络延迟，请稍后继续", {time: 800, icon: 2});
                        }
                    })
                }
            },
            /**
             * 下载文件
             */
            download: function (cid) {
                var obj = {
                    downloadType: "DOWNLOAD",
                    areaId: areaId
                }
                window.open(constant.server_path + "/core/attach/download/" + cid + "?" + Utils.formatParams(obj));
            },
        };
        window.fns = fns;
        init();
    });

    //入口方法
    function init() {
        var vm = new Vue({
            el: '#contact',
            data: {
                enums: "",
                catalogs: ""
            }
        })
        var vm1 = new Vue({
            el: '#download',
            data: {
                attach: ""
            }
        });
        AMap.plugin('AMap.CitySearch', function () {
            var citySearch = new AMap.CitySearch()
            citySearch.getLocalCity(function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    areaId = result.adcode;
                    Utils.getSimpleJson({
                        url: "/core/systemRecord/save",
                        noAsync: true,
                        params: {
                            areaId: areaId
                        },
                        fn: function (data) {
                            // $("#pageView").html("今日浏览量：" + data.todayPageView + " 总浏览量: " + data.allPageView);
                        }
                    })
                }
            })
        })
        modifyByJs();
        // //获取单个文件
        Utils.getSimpleJson({
            url: "/core/attach/single",
            params: {
                fileType: "FILE_TYPE_JAR",
            },
            fn: function (data) {
                vm1.attach = data;
            }
        })
        Utils.getSimpleJson({
            url: "/core/enum/list",
            params: {
                path: "ContactTypeEnum"
            },
            fn: function (data) {
                vm.enums = data
            }
        })
        let headers = Utils.getHeaders();
        if (headers && headers.session) {
            $("#loginLi").css("display", "none");
            $("#signInLi").css("display", "none");
            $("#userInfo").css("display", "block");
            $("#userInfo").find("span").html("欢迎：" + headers.name);
            $("#logOutLi").css("display", "block");
        }

    }

    function modifyByJs() {
        //鼠标滑过标签显示
        var lis = $("#flag ul li");
        var imgs = $("#flagImg img");
        $(lis).each(function (index) {
            var img = $(imgs).get(index);
            $(this).mouseover(function () {
                $(img).fadeIn(500);
            }).mouseleave(function () {
                $(img).fadeOut(200);
            })
        })
    }

})(document, window, jQuery, Utils);